<?php
/**
 * @Author:      lyt
 * @DateTime:    2016-01-26 10:08:38
 * @Description: 用于记录订单的出库记录
 */
class MaintenanceReturnOrder extends Eloquent
{
    protected $table = "maintenance_return_order";

    /**
     * @Author:      zwh
     * @DateTime:    2016-3-5 11:02:26
     * @Description: 退货
     *
     */
    public static function returnOrders($moo_ids,$order_id,$shop_id){
        return DB::transaction(function () use ($moo_ids,$order_id,$shop_id) {
            foreach ($moo_ids as $moo_id){
                $moo=MaintenanceOutOrder::where("id",$moo_id['out_order_id'])->whereIn("status",array(0,1))->first();
                if($moo){
                    DB::rollback();
                    $response = Lang::get("scs_error.maintenance_return_error_70001");
                    return $response;
                }                    
                if($moo_id['number']<=0){
                    DB::rollback();
                    $response = Lang::get("scs_error.maintenance_return_number_error_70004");
                    return $response;
                }
                $mro=MaintenanceReturnOrder::where('out_order_id',$moo_id['out_order_id'])->whereIn('status',array(0,1))->sum('number');
                $total=$mro+$moo_id['number'];
                $moo=MaintenanceOutOrder::find($moo_id['out_order_id']);
                if($moo->product_type=='promotion'){
                    DB::rollback();
                    $response = Lang::get("scs_error.maintenance_return_error_70003");
                    return $response;
                }
                if($total>$moo->number){//如果 该批次的 退货总数大于出库总数
                    DB::rollback();
                    $response = Lang::get("scs_error.maintenance_return_error_70002");
                    // if($moo->type==0){
                    //     $si=SupplyItem::find($moo->item_id);
                    //     $ss=SupplySeries::find($si->series_id);
                    //     $name=$ss->name.' '.$si->model;
                    // }else{
                        $si=SupplyItem::find($moo->item_id);
                        $name=$si->name;
                    // }
                    $response["result"]=array($name." 数量".$moo_id['number']."超过可退库数量");
                    return $response;
                }
                //记录 库存退货记录
                $mro=new MaintenanceReturnOrder();
                $mro->shop_id=$shop_id;
                $mro->status=0;
                $mro->type=$moo->type;
                $mro->item_id=$moo->item_id;
                $mro->product_type=$moo->product_type;
                $mro->product_item_id=$moo->product_item_id;
                $mro->order_id=$moo->order_id;
                $mro->number=$moo_id['number'];
                $mro->out_order_id=$moo_id['out_order_id'];
                $mro->save();
            }
            return array('code' => 0, 'msg' => 'ok','result'=>array('order_id',$order_id));
        });
    }


    /**
     * @Author:      lyt
     * @DateTime:    2016-10-24 11:17:34
     * @Description: 用于订单的一键退库
     */
    public static function onekeyReturnOutOrder($moo_ids,$order_id,$sc_user){
        return DB::transaction(function () use ($moo_ids,$order_id,$sc_user) {
            $return_order_arr=array();
            foreach ($moo_ids as $moo_id){
                $moo=MaintenanceOutOrder::where("id",$moo_id['out_order_id'])->whereIn("status",array(0,1))->lockForUpdate()->first();
                if($moo){
                    DB::rollback();
                    $response = Lang::get("scs_error.maintenance_return_error_70001");
                    return $response;
                }                    
                if($moo_id['number']<=0){
                    DB::rollback();
                    $response = Lang::get("scs_error.maintenance_return_number_error_70004");
                    return $response;
                }
                $mro=MaintenanceReturnOrder::where('out_order_id',$moo_id['out_order_id'])->whereIn('status',array(0,1))->sum('number');
                $total=$mro+$moo_id['number'];
                $moo=MaintenanceOutOrder::find($moo_id['out_order_id']);
                if($moo->product_type=='promotion'){
                    DB::rollback();
                    $response = Lang::get("scs_error.maintenance_return_error_70003");
                    return $response;
                }
                if($total>$moo->number){//如果 该批次的 退货总数大于出库总数
                    DB::rollback();
                    $response = Lang::get("scs_error.maintenance_return_error_70002");
                    // if($moo->type==0){
                    //     $si=SupplyItem::find($moo->item_id);
                    //     $ss=SupplySeries::find($si->series_id);
                    //     $name=$ss->name.' '.$si->model;
                    // }else{
                        $si=SupplyItem::find($moo->item_id);
                        $name=$si->name;
                    // }
                    $response["result"]=array($name." 数量".$moo_id['number']."超过可退库数量");
                    return $response;
                }
                //记录 库存退货记录
                $mro=new MaintenanceReturnOrder();
                $mro->shop_id=$sc_user->shop_id;
                $mro->status=0;
                $mro->type=$moo->type;
                $mro->item_id=$moo->item_id;
                $mro->product_type=$moo->product_type;
                $mro->product_item_id=$moo->product_item_id;
                $mro->order_id=$moo->order_id;
                $mro->number=$moo_id['number'];
                $mro->out_order_id=$moo_id['out_order_id'];
                $mro->save();
                $return_order_arr[]=$mro;
            }
            $response=MaintenanceStorageList::implementOrder($return_order_arr,$sc_user);
            return $response;
        });
    }

    /**
     * @Author:      lyt
     * @DateTime:    2016-03-07 14:58:52
     * @Description: 用于拒绝 订单的退库纪录
     */
    public static function refuseOrder($return_orders){
        return DB::transaction(function () use ($return_orders) {
            foreach ($return_orders as $_ro) {
                //更改 退库申请 为失败
                $_ro->status=2;
                $_ro->save();
            }
        });
    }
}